const { Paragraph, TextRun, ShadingType } = require("docx");
const { getAlignment, getIndentValue, generateUnderlineText, getPrefixSymbol, mapFontSizeToHalfPoints } = require("../common/utils");

// 生成竖向选项字段
function generateVerticalOptions(field, baseIndent = 0) {
    const value = field.value || {};
    const fieldName = value.fieldName || '';
    const options = value.options || [];
    const prefixSymbol = value.prefixSymbol || 'none';
    const alignment = value.alignment || 'left';
    const sizeHalfPoints = mapFontSizeToHalfPoints(value.fontSize);
    const isBold = value.isBold || false;
    
    // 计算最终缩进值
    const finalIndent = getIndentValue(alignment, baseIndent);
    const elements = [];
    
    // 如果有字段名称，先创建字段名称段落
    if (fieldName.trim()) {
        const nameChildren = [];
        
        // 添加前缀符号
        const symbol = getPrefixSymbol(prefixSymbol);
        if (symbol) {
            nameChildren.push(new TextRun({
                text: symbol,
                size: sizeHalfPoints,
                font: "SimSun" // 宋体
            }));
        }
        
        // 添加字段名称
        nameChildren.push(new TextRun({
            text: fieldName,
            size: sizeHalfPoints,
            bold: isBold,
            font: "SimSun"
        }));
        
        elements.push(new Paragraph({
            indent: { left: finalIndent },
            alignment: getAlignment(value.labelAlign || alignment),
            spacing: { 
                before: 100, 
                after: 50,
                line: 360, // 行间距
                lineRule: "atLeast"
            },
            children: nameChildren
        }));
    }
    
    // 处理选项 - 每个选项单独一行
    if (options.length === 0) {
        // 如果没有选项，创建一个空的复选框行
        elements.push(new Paragraph({
            indent: { left: finalIndent + (fieldName.trim() ? 360 : 0) }, // 如果有字段名称，选项增加缩进
            alignment: getAlignment(alignment),
            spacing: { 
                before: 50, 
                after: 50,
                line: 360,
                lineRule: "atLeast"
            },
            children: [
                new TextRun({
                    text: "☐",
                    size: sizeHalfPoints + 5,
                    font: "SimSun"
                }),
                new TextRun({ text: ' ', size: sizeHalfPoints, font: 'SimSun' })
            ]
        }));
    } else {
        // 渲染每个选项，垂直排列
        options.forEach((option, index) => {
            const optionChildren = [];
            
            // 添加复选框
            optionChildren.push(new TextRun({
                text: "☐",
                size: sizeHalfPoints + 5,
                font: "SimSun"
            }));
            optionChildren.push(new TextRun({ text: ' ', size: sizeHalfPoints, font: 'SimSun' }));
            
            // 添加选项文本
            if (option.text && option.text.trim()) {
                optionChildren.push(new TextRun({
                    text: option.text,
                    size: sizeHalfPoints,
                    font: "SimSun"
                }));
            }
            
            // 添加下划线（如果启用）
            if (option.showLine) {
                const lineLength = Math.max(1, option.lineLength || 10);
                const underlineText = generateUnderlineText(lineLength);
                
                optionChildren.push(new TextRun({
                    text: underlineText,
                    size: sizeHalfPoints,
                    font: "SimSun"
                }));
            }
            
            // 创建选项段落
            elements.push(new Paragraph({
                indent: { left: finalIndent + (fieldName.trim() ? 360 : 0) }, // 如果有字段名称，选项增加缩进
                alignment: getAlignment(alignment),
                spacing: { 
                    before: 50, 
                    after: index === options.length - 1 ? 100 : 50, // 最后一个选项后增加更多间距
                    line: 360,
                    lineRule: "atLeast"
                },
                children: optionChildren
            }));
        });
    }
    
    return elements;
}

module.exports = {
    generateVerticalOptions
}; 